package com.ss.android.article.browser.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.a.a.b;
import com.a.a.e;
import com.a.a.s;
import com.a.a.t;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.BrowserPage;
import com.ss.android.article.browser.FeedFragment;
import com.ss.android.article.browser.R;
import com.ss.android.article.browser.c.SpecialPageHelper;
import com.ss.android.article.browser.d;
import com.ss.android.article.browser.database.b.HistoryManager;
import com.ss.android.article.browser.search.SearchHistoryAdapter;
import com.ss.android.article.browser.search.SharedPreferenceHelper;

import java.util.List;

import inline.NetworkDemo;

public final class ba implements
        View.OnFocusChangeListener,
        View.OnKeyListener,
        View.OnTouchListener,
        TextView.OnEditorActionListener,
        com.ss.android.article.browser.main.BrowserBehavior.BrowserBehavior$b {
    final View mLLSearchBox;
    final SearchEditTextView mEtSearchInput;
    private final Context d;
    private final SharedPreferenceHelper mSharedPreferenceHelper;
    private final FeedFragment mFeedFragment;
    private final d g;
    private final View h;
    private final TabInspector i;
    private final TextView mTvSearchCancel;
    private final View mIvClearSearchInput;
    private final ListView mListViewSearchHistory;
    private final HistoryManager mHistoryManager;
    private final View mTvClearBrowserHistory;
    public a c;
    private String o;
    private SearchHistoryAdapter mSearchHistoryAdapter;

    public ba(Context context, View view, final FeedFragment feedFragment) {
        this.d = context;
        this.h = view;
        if (TabInspector.getInstance() == null) {
            TabInspector.a((BrowserPage) context, ((Activity) context).getIntent());
        }

        this.i = TabInspector.getInstance();
        this.mHistoryManager = HistoryManager.getInstance();
        this.mLLSearchBox = view.findViewById(com.ss.android.article.browser.R.id.ll_search_box);
        this.mTvSearchCancel = view.findViewById(com.ss.android.article.browser.R.id.tv_search_cancel);
        this.mIvClearSearchInput = view.findViewById(com.ss.android.article.browser.R.id.iv_clear_search_input);
        this.mTvClearBrowserHistory = view.findViewById(com.ss.android.article.browser.R.id.tv_clear_browser_history);
        this.mEtSearchInput = view.findViewById(com.ss.android.article.browser.R.id.et_search_input);
        this.mListViewSearchHistory = view.findViewById(com.ss.android.article.browser.R.id.listview_search_history);
        this.mFeedFragment = feedFragment;
        this.mSharedPreferenceHelper = SharedPreferenceHelper.getInstance();
        this.g = com.ss.android.article.browser.d.getInstance();
        this.mTvSearchCancel.setOnClickListener(new View.OnClickListener() {

            public final void onClick(View arg5) {
                if (ba.a(ba.this, R.string.enter).equals(ba.a(ba.this).getText().toString())) {
                    ba.this.onEditorAction(ba.b(ba.this), 3, null);
                } else {
                    feedFragment.onGoBackClick();
                }
            }
        });
        this.mSearchHistoryAdapter = new SearchHistoryAdapter(this.d);
        this.mSearchHistoryAdapter.mTvClearBrowserHistory = this.mTvClearBrowserHistory;
        SearchHistoryAdapter v2 = this.mSearchHistoryAdapter;
        mEtSearchInput.a = mListViewSearchHistory;
        v2.b = ((SearchHistoryAdapter.b$b) mEtSearchInput);
        mEtSearchInput.b = v2.getFilter();
        mListViewSearchHistory.setAdapter(v2);
        s v0_1 = this.mHistoryManager.d();
        ((e) v0_1).b = android.support.design.a.c();
        ((e) v0_1).c = android.support.design.a.b();
        v0_1.a(new t() {

            public final void a(@Nullable Object arg3) {
                super.a(arg3);
                if (arg3 == null || ((List) arg3).size() <= 0) {
                    UIUtils.setVisibility(ba.c(ba.this), 8);
                }

                ba.d(ba.this).a(((List) arg3));
            }
        });
        this.mEtSearchInput.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public final void onItemClick(AdapterView arg4, View arg5, int arg6, long arg7) {
                String v0_1;
                String v1 = null;
                CharSequence v0 = ((TextView) arg5.findViewById(com.ss.android.article.browser.R.id.ac4)).getText();
                if (v0 != null) {
                    v1 = v0.toString();
                }

                if (v1 == null || (v1.startsWith(ba.a(ba.this, R.string.yu)))) {
                    v0 = ((TextView) arg5.findViewById(com.ss.android.article.browser.R.id.title)).getText();
                    if (v0 != null) {
                        v0_1 = v0.toString();
                    } else {
                        label_53:
                        v0_1 = v1;
                    }
                } else {
                    v0_1 = v1;
                }

                if (v0_1 != null) {
                    ba.b(ba.this).a(((CharSequence) v0_1), false);
                    ba.a(ba.this, v0_1);
                    android.support.design.a.a(ba.g(ba.this), ba.b(ba.this).getWindowToken());
                    ba.h(ba.this).n();
                    if (android.support.design.a.isTextEmpty(ba.b(ba.this).getText().toString())) {
                        com.ss.android.g.a.a$a v1_1 = new com.ss.android.g.a.a$a("click_search_history");
                        String v2 = "search_type";
                        v0_1 = SpecialPageHelper.a(v0_1) ? "web" : "search";
                        v1_1.a(v2, v0_1).a().a();
                    }
                }
            }
        });
        this.mTvClearBrowserHistory.setOnClickListener(new View.OnClickListener() {

            public final void onClick(View arg3) {
                com.a.a.a v0 = ba.e(ba.this).b();
                ((e) v0).b = android.support.design.a.c();
                ((e) v0).c = android.support.design.a.b();
                v0.a(new b() {

                    public final void a() {
                        super.a();
                        UIUtils.setVisibility(ba.c(ba.this), 8);
                        SearchHistoryAdapter adapter = ba.d(ba.this);
                        adapter.mBrowserHistoryList.clear();
                        adapter.notifyDataSetChanged();
                    }

                    public final void onError(@NonNull Throwable arg3) {
                        Log.e("SearchHelper", "Exception while updating history", arg3);
                    }
                });
            }
        });
        this.mEtSearchInput.setOnTextChangedListener(new TextWatcher() {

            public final void afterTextChanged(Editable editable) {
                int v2 = 8;
                int v1 = 0;
                int v0 = editable == null || editable.length() == 0 ? 1 : 0;
                View v3 = ba.f(ba.this);
                if (editable == null || editable.length() <= 0) {
                    v1 = v2;
                }

                UIUtils.setVisibility(v3, v1);
                if (v0 == 0) {
                    UIUtils.setVisibility(ba.c(ba.this), v2);
                }

                TextView v1_1 = ba.a(ba.this);
                v0 = v0 != 0 ? R.string.cancel : R.string.enter;
                v1_1.setText(v0);
            }

            public final void beforeTextChanged(CharSequence arg1, int arg2, int arg3, int arg4) {
            }

            public final void onTextChanged(CharSequence arg1, int arg2, int arg3, int arg4) {
            }
        });
        this.mIvClearSearchInput.setOnClickListener(new View.OnClickListener() {

            public final void onClick(View arg4) {
                ba.b(ba.this).a("", true);
            }
        });
    }

    static TextView a(ba arg1) {
        return arg1.mTvSearchCancel;
    }

    static String a(ba arg1, int arg2) {
        return arg1.d.getString(arg2);
    }

    static void a(ba arg0, String arg1) {
        arg0.a(arg1);
    }

    static SearchEditTextView b(ba arg1) {
        return arg1.mEtSearchInput;
    }

    static View c(ba arg1) {
        return arg1.mTvClearBrowserHistory;
    }

    static SearchHistoryAdapter d(ba arg1) {
        return arg1.mSearchHistoryAdapter;
    }

    static HistoryManager e(ba arg1) {
        return arg1.mHistoryManager;
    }

    static View f(ba arg1) {
        return arg1.mIvClearSearchInput;
    }

    static Context g(ba arg1) {
        return arg1.d;
    }

    static FeedFragment h(ba arg1) {
        return arg1.mFeedFragment;
    }

    public final void a(String arg6, String arg7) {
        if (arg6 != null && this.mLLSearchBox != null && !this.mLLSearchBox.hasFocus()) {
            SearchEditTextView v0 = this.mEtSearchInput;
            d v1 = this.g;
            if (SpecialPageHelper.isSpecialUrl(arg6)) {
                arg6 = "";
            } else {
                switch (v1.b.mSharedPreferences.getInt("urlContent", 0)) {
                    case 1: {
                        break;
                    }
                    case 2: {
                        arg6 = !TextUtils.isEmpty(((CharSequence) arg7)) ? arg7 : v1.a;
                        break;
                    }
                    default:
                        arg7 = NetworkDemo.a(arg6);
                        if (arg7 != null) {
                            arg6 = arg7;
                        }

                }
            }

            label_12:
            v0.setText(arg6);
        }
    }

    private void a(@NonNull String arg3) {
        if (!android.support.design.a.isTextEmpty(arg3)) {
            String v0 = this.o + "%s";
            if (this.c != null) {
                this.c.loadUrl(arg3, v0);
            }

            android.support.design.a.a(this.d, this.mEtSearchInput.getWindowToken());
            if (SpecialPageHelper.isSpecialUrl(arg3)) {
                return;
            }

            com.a.a.a v0_1 = com.a.a.a.a(new com.ss.android.article.browser.database.b.d(this.mHistoryManager, arg3, arg3));
            ((e) v0_1).b = android.support.design.a.c();
            v0_1.a(new b() {
                public final void onError(@NonNull Throwable arg3) {
                    Log.e("SearchHelper", "Exception while updating history", arg3);
                }
            });
        }
    }

    public final void a() {
        com.ss.android.article.browser.search.a.b v0_1;
        switch (this.mSharedPreferenceHelper.mPreferenceManager.getSearch()) {
            case 0: {
                v0_1 = new com.ss.android.article.browser.search.a.a();
                break;
            }
            default: {
                v0_1 = new com.ss.android.article.browser.search.a.c();
                break;
            }
        }
        this.o = v0_1.a;
    }

    public final void a(float arg3) {
        UIUtils.setVisibility(this.h, 8);
    }

    public final void a(boolean arg6) {
        int v2 = 8;
        View v3 = this.h;
        int v0 = arg6 ? 0 : v2;
        UIUtils.setVisibility(v3, v0);
        if (arg6) {
            UIUtils.setVisibility(this.h, 0);
        }

        v3 = this.mLLSearchBox;
        v0 = arg6 ? 0 : v2;
        UIUtils.setVisibility(v3, v0);
        if (!arg6) {
            android.support.design.a.a(this.d, this.mEtSearchInput.getWindowToken());
            this.mEtSearchInput.setText("");
        } else {
            this.mEtSearchInput.setFocusable(true);
            this.mEtSearchInput.setClickable(true);
            this.mEtSearchInput.setFocusableInTouchMode(true);
            this.mEtSearchInput.setOnKeyListener(((View.OnKeyListener) this));
            this.mEtSearchInput.setOnEditorActionListener(((TextView.OnEditorActionListener) this));
            this.mEtSearchInput.requestFocus();
            android.support.design.a.a(this.d, this.mEtSearchInput);
        }

        if (arg6) {
            new com.ss.android.g.a.a$a("click_wap_search").a().a();
        }

        if (arg6) {
            UIUtils.setVisibility(this.h, 0);
        }

        ListView v3_1 = this.mListViewSearchHistory;
        v0 = arg6 ? 0 : v2;
        UIUtils.setVisibility(((View) v3_1), v0);
        BrowserPage v0_1 = this.i.mBrowserPage;
        if (!arg6) {
            v2 = 0;
        }

        v0_1.setBottomTabVisible(v2);
        if (!arg6 || !this.mFeedFragment.o()) {
            this.mEtSearchInput.setText("");
        } else {
            this.mEtSearchInput.setText(this.mFeedFragment.getUrl());
        }
    }

    public final void b(float arg1) {
    }

    public final boolean b() {
        boolean v0 = this.h == null || this.h.getVisibility() != View.VISIBLE ? false : true;
        return v0;
    }

    public final boolean onEditorAction(TextView arg4, int arg5, KeyEvent arg6) {
        boolean v0;
        if (arg5 == 2 || arg5 == 6 || arg5 == 5 || arg5 == 4 || arg5 == 3 || arg6.getAction() == 66) {
            android.support.design.a.a(this.d, this.mEtSearchInput.getWindowToken());
            this.a(this.mEtSearchInput.getText().toString());
            com.ss.android.g.a.a$a v1 = new com.ss.android.g.a.a$a("wap_search_confirm");
            String v2 = "search_type";
            String v0_1 = SpecialPageHelper.a(this.mEtSearchInput.getText().toString()) ? "web" : "search";
            v1.a(v2, v0_1).a().a();
            this.mFeedFragment.n();
            v0 = true;
        } else {
            v0 = false;
        }

        return v0;
    }

    public final void onFocusChange(View arg3, boolean arg4) {
        if (!arg4) {
            android.support.design.a.a(this.d, this.mEtSearchInput.getWindowToken());
        }
    }

    public final boolean onKey(View arg4, int arg5, KeyEvent arg6) {
        boolean v0;
        switch (arg5) {
            case 66: {
                ((InputMethodManager) this.d.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(arg4.getWindowToken(), 0);
                this.a(this.mEtSearchInput.getText().toString());
                this.mFeedFragment.n();
                v0 = true;
                break;
            }
            default: {
                v0 = false;
                break;
            }
        }

        return v0;
    }

    @SuppressLint(value = {"ClickableViewAccessibility"})
    public final boolean onTouch(View arg6, MotionEvent arg7) {
        boolean v0 = true;
        if (this.mEtSearchInput.getCompoundDrawables()[2] != null) {
            int v2 = arg7.getX() > (((float) (this.mEtSearchInput.getWidth() - this.mEtSearchInput.getPaddingRight()))) ? 1 : 0;
            if (v2 == 0) {
                v0 = false;
            } else {
                if (arg7.getAction() != 1) {
                    return v0;
                }
                if (!this.mEtSearchInput.hasFocus()) {
                    return v0;
                }
                this.mEtSearchInput.setText("");
            }
        } else {
            label_28:
            v0 = false;
        }

        return v0;
    }

    public interface a {
        void loadUrl(String arg1, String arg2);
    }
}

